package cn.jbolt.core.actionreport;
import cn.jbolt.core.base.config.JBoltConfig;
import cn.jbolt.core.util.JBoltIpUtil;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.Action;
import com.jfinal.core.ActionReporter;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.JsonRequest;
import javassist.ClassPool;
import javassist.CtMethod;
import javassist.NotFoundException;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

/**
 * JBolt 自定义Action执行报告
 */
public class JBoltActionReport extends ActionReporter {
    protected static final String JBOLT_TITLE = "\nJBolt Action Report -------- ";
    @Override
    public void report(String target, Controller controller, Action action) {
        CtMethod ctMethod = null;
        int lineNumber = 1;
        if(JBoltConfig.ACTION_REPORT_LINE_NUMBER_ENABLE) {
            try {
                ctMethod = ClassPool.getDefault().getMethod(controller.getClass().getName(), action.getMethodName());
                lineNumber = ctMethod.getMethodInfo().getLineNumber(0);
            } catch (NotFoundException ignored) {
            }
        }
        StringBuilder sb = new StringBuilder(JBOLT_TITLE).append(sdf.get().format(new Date())).append(" --------------------------\n");
        sb.append("Req Url     : ").append(target).append('\n');
        sb.append("Req IP      : ").append(JBoltIpUtil.getIp(controller.getRequest())).append('\n');
        sb.append("Req Method  : ").append(controller.getRequest().getMethod()).append('\n');
        //sb.append("Req CrossOrigin  : ").append(JBoltControllerKit.checkNeedCrossOrigin(controller, action.getMethod())?"Need Process Cross Origin":"-").append('\n');
        Class<? extends Controller> cc = action.getControllerClass();
        sb.append("Controller  : ").append(cc.getName()).append(".(").append(cc.getSimpleName()).append(".java:").append(lineNumber).append(')').append("\n");
        sb.append("Action      : ").append(action.getMethodName()).append("\n");

        String urlParas = controller.getPara();
        if (urlParas != null) {
            sb.append("Url Params  : ").append(urlParas).append('\n');
        }

        Interceptor[] inters = action.getInterceptors();
        if (inters.length > 0) {
            sb.append("Interceptor : ");
            lineNumber = 1;
            ctMethod = null;
            Class<? extends Interceptor> ic;
            for (int i=0; i<inters.length; i++) {
                lineNumber = 1;
                ctMethod = null;
                if (i > 0) {
                    sb.append("\n              ");
                }
                ic = inters[i].getClass();
                if(JBoltConfig.ACTION_REPORT_LINE_NUMBER_ENABLE) {
                    try {
                        ctMethod = ClassPool.getDefault().getMethod(ic.getName(), "intercept");
                        lineNumber = ctMethod.getMethodInfo().getLineNumber(0);
                    } catch (NotFoundException ignored) {
                    }
                }
                sb.append(ic.getName()).append(".(").append(ic.getSimpleName()).append(".java:").append(lineNumber).append(')');
            }
            sb.append('\n');
        }

        // print all parameters
        HttpServletRequest request = controller.getRequest();
        if (request instanceof JsonRequest) {
            buildJsonPara(controller, sb);
        } else {
            buildPara(controller, sb);
        }

        sb.append("--------------------------------------------------------------------------------\n");

        try {
            writer.write(sb.toString());
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}


